using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace GameFramework
{
    public class AnimationFrames
    {
        #region Feilds

        private string _name;
        public string Name
        {
            get
            {
                return _name;
            }
            set
            {
                _name = value;
            }
        }

        public int ID;

        // toc do animation
        private int _frameRate;
        public int FrameRate
        {
            get
            {
                return _frameRate;
            }
            set
            {
                _frameRate = value;
                _delayFrameTime = 1000 / _frameRate;
            }
        }


        private bool _isLoop;
        public bool IsLoop
        {
            get
            {
                return _isLoop;
            }
            set
            {
                _isLoop = value;
                _isStop = !value;
            }
        }

 
        private List<int> _animationFrames;
        public List<int> FramesList
        {
            get
            {
                return _animationFrames;
            }
            set
            {
                _animationFrames = value;
            }
        }

        public Dictionary<int, FrameModule> Frames;

        private List<Vector2> _offsets;
        public List<Vector2> Offsets
        {
            get
            {
                return _offsets;
            }
            set
            {
                _offsets = value;
            }
        }

        private Vector2 _position;
        public Vector2 Position
        {
            get
            {
                return _position;
            }
            set
            {
                _position = value;
            }
        }


        private int _currentFrameIndex;
        public int CurrentFrameIndex
        {
            get
            {
                return _currentFrameIndex;
            }
            set
            {
                _currentFrameIndex = value;
            }
        }

        private int _delayFrameTime;
        public int DelayFrameTime
        {
            get
            {
                return _delayFrameTime;
            }
            set
            {
                _delayFrameTime = value;
            }
        }

        private bool _isStop;
        public bool IsStop
        {
            get
            {
                return _isStop;
            }
            set
            {
                _isStop = value;
            }
        }

        
        public int NumOfAnimFrames
        {
            get
            {
                return _animationFrames.Count;
            }           
        }
        #endregion

        #region Variable
        private double _progressTime;
        #endregion

        #region Methods
        public AnimationFrames()
        {
            _animationFrames = new List<int>();
            _delayFrameTime = 0;
            _progressTime = 0;
            _frameRate = 0;
            _isLoop = false;
            _name = "";
            _isStop = false;
            _offsets = new List<Vector2>();
            _position = new Vector2(float.MinValue, float.MinValue);
            Reset();
        }

        public void AddNewFrame(int frameID, Vector2 offset)
        {
            _animationFrames.Add(frameID);
            _offsets.Add(offset);
        }


        public void Reset()
        {
            _currentFrameIndex = 0;
            _isStop = false;
        }

        public void Update(GameTime gameTime)
        {           

            if (_isStop)
                return;

            _progressTime += gameTime.ElapsedGameTime.TotalMilliseconds;

            if (_progressTime > _delayFrameTime)
            {
                _progressTime = 0;

                _currentFrameIndex++;//frame ke tiep

                if (_currentFrameIndex >= NumOfAnimFrames)//frame cuoi cung
                {
                    if (_isLoop)
                    {
                        _isStop = true;
                        _currentFrameIndex = 0;
                        return;
                    }
                    else
                    {
                        _currentFrameIndex = 0;//tro ve frame 0
                    }
                }               
            }
        }

        public void Draw(Vector2 position)
        {
            Frames[_animationFrames[_currentFrameIndex]].Draw(new Vector2(100,100)+_offsets[_currentFrameIndex],Vector2.Zero);
        }
        #endregion
    }
}
